# Maintainer: Caleb Maclennan <caleb@alerque.com>

pkgbase=lua-luautf8
_rockname=${pkgbase#lua-}
pkgname=("$pkgbase" "lua51-$_rockname" "lua52-$_rockname" "lua53-$_rockname")
pkgver=0.1.5
_rockrel=1
pkgrel=2
pkgdesc='Add UTF-8 support to Lua, compatible with Lua string module'
arch=(loong64 x86_64)
url="https://github.com/starwing/$_rockname"
license=(MIT)
makedepends=(lua
             lua51
             lua52
             lua53
             luarocks)
options=(debug)
_archive="$_rockname-$pkgver"
_rock="$_archive-$_rockrel.linux-$CARCH.rock"
_rockspec="$_archive-$_rockrel.rockspec"
source=("$url/archive/$pkgver/$_archive.tar.gz")
sha256sums=('f4bddecc87521c53d37c09b9e9edd70a4ab35b0074040f303cbe3a0e088af21c')

prepare() {
	cd "$_archive"
	sed -e "s/0\.1\.4/$pkgver/g" -u "rockspecs/$_rockspec"
}

build() {
	cd "$_archive"
	for LUAVER in 5.{1,2,3,4}; do
		luarocks --lua-version "$LUAVER" \
			make --pack-binary-rock --deps-mode none -- "rockspecs/$_rockspec"
		install -Dm0644 -t "lua-$LUAVER/" "$_rock"
	done
}

_package() {
	cd "$_archive"
	depends=("${pkgname%-*}")
	provides=("${pkgname/-lua/-}=$pkgver")
	replaces=("${pkgname/-lua/-}")
	luarocks --lua-version "$1" --tree "$pkgdir/usr/" \
		install --deps-mode none --no-manifest -- "lua-$1/$_rock"
	install -Dm0644 -t "$pkgdir/usr/share/licenses/$pkgname/" LICENSE
}

package_lua-luautf8() {
	_package 5.4
}

package_lua53-luautf8() {
	_package 5.3
}

package_lua52-luautf8() {
	_package 5.2
}

package_lua51-luautf8() {
	_package 5.1
}
